package com.shixi.shixitraining.shixitraining.ui.tabhome.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.shixi.shixitraining.shixitraining.R;
import com.shixi.shixitraining.shixitraining.base.BaseActivity;
import com.shixi.shixitraining.shixitraining.http.CallbackImple;
import com.shixi.shixitraining.shixitraining.http.HttpUtil;
import com.shixi.shixitraining.shixitraining.ui.tabhome.adapter.TrainAdapter;
import com.shixi.shixitraining.shixitraining.ui.tabhome.entity.CompositeBean;
import com.shixi.shixitraining.shixitraining.ui.tabhome.entity.CourseBean;
import com.shixi.shixitraining.shixitraining.ui.tabhome.entity.TrainAuthenBean;
import com.shixi.shixitraining.shixitraining.util.TitleUtil;
import com.shixi.shixitraining.shixitraining.util.ToastUtil;
import com.shixi.shixitraining.shixitraining.view.DropDownMenu;
import com.shixi.shixitraining.shixitraining.view.recycleview.BaseAdapter;
import com.shixi.shixitraining.shixitraining.view.recycleview.BaseRecycleHolder;
import com.shixi.shixitraining.shixitraining.view.recycleview.RecycleItemClickListener;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;

import static com.shixi.shixitraining.shixitraining.constant.Constants.TRAIN;

public class HomeGrideAuthenActivity extends BaseActivity implements View.OnClickListener {

    private DropDownMenu dropDownMenu;
    private String tabs[] = {"综合排序", "课程属性", "专业类型"};
    private List<View> popups = new ArrayList<>();
    private int firstItem = 0;
    private int selectedItem = 0;
    private SmartRefreshLayout mRefresh;
    private RecyclerView mRcview;
    private TextView mNoData;

    private SmartRefreshLayout refreshLayout;
    private int currentPage = 1;
    private List<TrainAuthenBean.DataBean.TypeBean> type;
    private List<TrainAuthenBean.DataBean.SortBean> sort;
    private List<TrainAuthenBean.DataBean.CourseBean> course;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home_gride_authen);
        initTitle();
        initBaseData();
//        initPopup();
    }

    private void initBaseData() {

        HttpUtil.getInstance().getApiInterface().TrainAuthen(TRAIN).enqueue(new CallbackImple<TrainAuthenBean>() {

            @Override
            public void onError(Call<TrainAuthenBean> call, Throwable t) {
                ToastUtil.showShortToast(HomeGrideAuthenActivity.this, t.getMessage());
            }

            @Override
            public void onSuccess(Call<TrainAuthenBean> call, TrainAuthenBean response) {
                if (response != null) {
                    if (response.getData() != null) {
                        final List<TrainAuthenBean.DataBean.ListsBean> baseData = response.getData().getLists();
                        type = response.getData().getType();
                        sort = response.getData().getSort();
                        course = response.getData().getCourse();
                        RecyclerView mRcview = (RecyclerView) findViewById(R.id.recview_jobs);
                        GridLayoutManager manager = new GridLayoutManager(HomeGrideAuthenActivity.this, 2);
                        manager.setOrientation(LinearLayoutManager.VERTICAL);
                        mRcview.setLayoutManager(manager);
                        TrainAdapter adapter = new TrainAdapter(HomeGrideAuthenActivity.this, baseData, Glide.with(HomeGrideAuthenActivity.this));
                        mRcview.setAdapter(adapter);
                        mRcview.addOnItemTouchListener(new RecycleItemClickListener(HomeGrideAuthenActivity.this) {
                            @Override
                            public void itemClickListener(View view, int position) {
                                TrainAuthenBean.DataBean.ListsBean listsBean = baseData.get(position);
                                String id = listsBean.getId();
                                Bundle bundle = new Bundle();
                                bundle.putString("trainId", id);
                                Intent intent = new Intent(HomeGrideAuthenActivity.this, AuthenDetailActivity.class);
                                intent.putExtras(bundle);
                                startActivity(intent);
                            }
                        });
                    } else {
                        ToastUtil.showShortToast(HomeGrideAuthenActivity.this, response.getMsg());
                    }
                } else {
                    ToastUtil.showShortToast(HomeGrideAuthenActivity.this, "数据为空");
                }
            }
        });

    }

    private void initPopup() {
        //综合排序

        View commposite = getLayoutInflater().inflate(R.layout.item_composite, null);
        RecyclerView commList = (RecyclerView) commposite.findViewById(R.id.rcview_poppup_composite);
        List<CompositeBean> data = new ArrayList<>();
//        data.add(new CompositeBean("综合排序"));
//        data.add(new CompositeBean("项目时间"));
//        data.add(new CompositeBean("价格降序"));
//        data.add(new CompositeBean("价格升序"));
        final CompositeAdapter comAdapter = new CompositeAdapter(this, data);
        commList.setAdapter(comAdapter);
        commList.addOnItemTouchListener(new RecycleItemClickListener(this) {
            @Override
            public void itemClickListener(View view, int position) {
                selectedItem = position;
                comAdapter.notifyDataSetChanged();
                dropDownMenu.closeMenu();
            }
        });


        //课程属性
        View session = getLayoutInflater().inflate(R.layout.item_session, null);
        RecyclerView recviewSession = (RecyclerView) session.findViewById(R.id.popup_session_list);
        List<CourseBean> sessionData = new ArrayList<>();
//        sessionData.add(new CompositeBean("不限"));
//        sessionData.add(new CompositeBean("企业管理"));
//        sessionData.add(new CompositeBean("职业认证"));

        final SessionAdapter sessionAdapter = new SessionAdapter(this, sessionData);
        recviewSession.setAdapter(sessionAdapter);
        recviewSession.addOnItemTouchListener(new RecycleItemClickListener(this) {
            @Override
            public void itemClickListener(View view, int position) {
                selectedItem = position;
                sessionAdapter.notifyDataSetChanged();
                dropDownMenu.closeMenu();
            }
        });


        //专业类型
        View industry = getLayoutInflater().inflate(R.layout.item_industry, null);
        RecyclerView rcviewIndustry = (RecyclerView) industry.findViewById(R.id.rcview_industry);
        List<CompositeBean> indstryData = new ArrayList<>();
//        indstryData.add(new CompositeBean("不限"));
//        indstryData.add(new CompositeBean("商务/市场"));
//        indstryData.add(new CompositeBean("运营类"));
//        indstryData.add(new CompositeBean("研发类"));
//        indstryData.add(new CompositeBean("运维类"));
//        indstryData.add(new CompositeBean("设计类"));
//        indstryData.add(new CompositeBean("医学"));

        final IndustryAdapter industryAdapter = new IndustryAdapter(this, indstryData);
        rcviewIndustry.setAdapter(industryAdapter);

        rcviewIndustry.addOnItemTouchListener(new RecycleItemClickListener(this) {
            @Override
            public void itemClickListener(View view, int position) {
                selectedItem = position;
                industryAdapter.notifyDataSetChanged();
                dropDownMenu.closeMenu();
            }
        });


        //职位列表

//        View mContent = getLayoutInflater().inflate(R.layout.joblist_content, null);
//        mRefresh = (SmartRefreshLayout) mContent.findViewById(R.id.refreshLayout);
//        mRcview = (RecyclerView) mContent.findViewById(R.id.recview_jobs);
//        mNoData = (TextView) mContent.findViewById(R.id.no_tv);
//        mRcview.setLayoutManager(new LinearLayoutManager(this));
//        popups.add(commposite);
//        popups.add(session);
//        popups.add(industry);
//        dropDownMenu.setDropDownMenu(Arrays.asList(tabs), popups, mContent);

    }


    private void initTitle() {

        TitleUtil titleUtil = new TitleUtil(findViewById(R.id.top_bar_layout));
        titleUtil.initTitle(4, R.id.top_bar_center_tv, R.id.top_bar_left_img, 0, R.drawable.icon_back,
                0, "培训认证", "", "");
        ImageView mBackImg = (ImageView) findViewById(R.id.top_bar_left_img);
        mBackImg.setOnClickListener(this);

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.top_bar_left_img:
                this.finish();
                break;
        }
    }


    //综合排序
    private class CompositeAdapter extends BaseAdapter<CompositeBean> {
        public CompositeAdapter(Context context, List<CompositeBean> data) {
            super(context, data);
        }

        @Override
        public void convert(BaseRecycleHolder holder, CompositeBean item, int position) {
            holder.setText(R.id.tv_compiste, item.getName());
            if (firstItem == position) {
                holder.setTextColor(R.id.tv_compiste, R.color.basepresscolor);
            } else {
                holder.setTextColor(R.id.tv_compiste, R.color.gray);
            }

        }


        @Override
        protected int getLayoutId(int viewType) {
            return R.layout.adapter_popup_composite;
        }
    }

//课程属性

    private class SessionAdapter extends BaseAdapter<CourseBean> {

        public SessionAdapter(Context context, List<CourseBean> data) {
            super(context, data);
        }

        @Override
        public void convert(BaseRecycleHolder holder, CourseBean item, int position) {
            holder.setText(R.id.tv_session, item.getName());
            if (firstItem == position) {
                holder.setTextColor(R.id.tv_session, R.color.basepresscolor);
            } else {
                holder.setTextColor(R.id.tv_session, R.color.gray);
            }

        }

        @Override
        protected int getLayoutId(int viewType) {
            return R.layout.adapter_popup_session;
        }

    }

    //专业类型
    private class IndustryAdapter extends BaseAdapter<CompositeBean> {
        public IndustryAdapter(Context context, List<CompositeBean> data) {
            super(context, data);
        }

        @Override
        public void convert(BaseRecycleHolder holder, CompositeBean item, int position) {
            holder.setText(R.id.tv_industry, item.getName());

            if (firstItem == position) {
                holder.setTextColor(R.id.tv_industry, R.color.basepresscolor);
            } else {
                holder.setTextColor(R.id.tv_industry, R.color.gray);
            }

        }

        @Override
        protected int getLayoutId(int viewType) {
            return R.layout.adapter_popup_industry;
        }


    }

}
